const { ref } = Vue
import { useFetch } from '../../../Scripts/hooks/useFetch.js'
const { ElMessage } = ElementPlus

export function useYPFirstContainerCheck() {
  const checkedContainerNoListTableRef = ref(null);
  const currentContainerNoListTableRef = ref(null);
  const firstCheckDialogVisible = ref(false);//叶片首件批次号选择弹出框
  const checkedContainerNoList = ref([]);//已指定的首件批次号
  const currentContainerNoList = ref([]);//当前批次子序号

  // 查询叶片首件批次号
  const getFirstContainerData = async configObj => {
    const { containerInfo, userInfo, baseAPI } = configObj;
    if (!containerInfo.containerstepdispatchid) {
      return ElMessage({ type: 'error', message: `请先选择需要报工的数据`, grouping: true });
    }
    firstCheckDialogVisible.value = true;

    const { data } = await useFetch({
      url: `${baseAPI}?actions=getFirstContainerData`,
      data: {
        type: 'getFirstContainerData',
        productid: containerInfo.productid,
        containerid: containerInfo.containerid,
        workflowstepid: containerInfo.workflowstepid,
        employeeid: userInfo.value.employeeid
      }
    });
    checkedContainerNoList.value = data.value.Table1;
    currentContainerNoList.value = data.value.Table2;
  }

  // 设置首件批次号
  const setFirstContainer = configObj => {
    const { reportDataInfo } = configObj;
    const checkedContainerSelectRows = checkedContainerNoListTableRef.value.getSelectionRows()
    const currentContainerSelectRows = currentContainerNoListTableRef.value.getSelectionRows();
    if (checkedContainerSelectRows.length > 0) {
      // 首件批次号
      reportDataInfo.firstcheckcontainerno = checkedContainerSelectRows[0].firstcheckcontainerno;
    }
    if (currentContainerSelectRows.length >= 2) {
      return ElMessage({ type: 'error', message: `首件批次号只能选择1条`, grouping: true });
    }
    if (currentContainerSelectRows.length === 0) {
      return ElMessage({ type: 'error', message: `请选择首件批次号`, grouping: true });
    }
    reportDataInfo.firstcheckcontainerno = currentContainerSelectRows[0].containername;
    firstCheckDialogVisible.value = false;
  }

  return {
    checkedContainerNoListTableRef,
    currentContainerNoListTableRef,
    firstCheckDialogVisible,
    checkedContainerNoList,
    currentContainerNoList,
    getFirstContainerData,
    setFirstContainer
  };
}